package system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import system.httpModel.Resources;
import system.service.ResourcesService;

import common.httpModel.EasyuiTreeNode;
import common.httpModel.Json;


@Controller
@RequestMapping("/resourcesController")
public class ResourcesController {
	private ResourcesService resourcesService;
	

	public ResourcesService getResourcesService() {
		return resourcesService;
	}

	@Autowired
	public void setResourcesService(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}


	/**
	 * 获取资源管理树
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(String id) {
		return resourcesService.tree(id);
	}
	
	/**
	 * 跳转到资源管理页面
	 */
	@RequestMapping(params="resources")
	public String resources(){
		return "/admin/resources";
	}
	
	/**
	 * 资源管理treegrid
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "treegrid")
	@ResponseBody
	public List<Resources> treegrid(String id) {
		return resourcesService.treegrid(id);
	}
	/**
	 * 添加资源
	 * 
	 * @param resources
	 * @return
	 */
	@RequestMapping(params = "add")
	@ResponseBody
	public Json add(Resources resources) {
		Json j = new Json();
		Resources r = resourcesService.add(resources);
		j.setSuccess(true);
		j.setMsg("添加成功!");
		return j;
	}

	/**
	 * 删除资源
	 * 
	 * @param resources
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public Json del(Resources resources) {
		Json j = new Json();
		if(resources.getMenuId()==null||"".equals(resources.getMenuId())){
			boolean rf=resourcesService.del(resources);	
			if(rf){
				j.setSuccess(true);
				j.setMsg("删除成功!");				
			}else{
				j.setSuccess(false);
				j.setMsg("请选择子菜单一个个删除!");			
			}
		}else{
			j.setSuccess(false);
			j.setMsg("不能删除，是菜单同步资源!请去菜单管理删除！");
		}
		
		return j;
	}

	/**
	 * 编辑资源
	 * 
	 * @param resources
	 * @return
	 */
	@RequestMapping(params = "edit")
	@ResponseBody
	public Json edit(Resources resources) {
		Json j = new Json();
		Resources r = null;
		if(resources.getMenuId()==null||"".equals(resources.getMenuId())){
			 r = resourcesService.edit(resources);
		}
		
		if(r!=null){
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		}else{
			j.setSuccess(false);
			j.setMsg("不能编辑，是菜单同步资源!请去菜单管理编辑");
		}

		return j;
	}
}
